<script setup lang="ts">
import { useSendMobileCode } from '@/composable'
import { loginByMobile, loginByPassword } from '@/services/user'
import { useUserStore } from '@/stores'
import { codeRules, mobileRules, passwordRules } from '@/utils/rules'
import axios from 'axios'
import { Toast } from 'vant'
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const store = useUserStore()
const router = useRouter()
const route = useRoute()

// 1. 是否同意协议
const agree = ref(false)

// 2. 表单校验
const mobile = ref('')
const password = ref('')
const show = ref(false)

// 3. 登录
const login = async () => {
  // 3.1 判断是否勾选协议
  if (!agree.value) return Toast('请勾选用户协议')
  // 3.2 调用接口
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)
  store.setUser(res.data)

  // 3.3 提示用户
  Toast.success('登录成功~')

  // 3.4 回跳页面
  router.replace((route.query.returnUrl as string) || '/user')
}

// 4. 登录方式切换
const isPass = ref(true)
const code = ref('')

// 5. 发送短信验证码
const { form, time, send } = useSendMobileCode(mobile, 'login')

// 6. 设置qq跳转的url地址
const qqUrl = `https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=${encodeURIComponent(
  import.meta.env.VITE_APP_CALLBACK + '/login/callback'
)}`

axios.get('/patient/message/list').then((res) => console.log(res))
</script>

<template>
  <div class="login-page">
    <!-- 导航 -->
    <cp-nav-bar
      right-text="注册"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ isPass ? '短信验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- form 表单 -->
    <van-form ref="form" autocomplete="off" @submit="login">
      <van-field
        name="mobile"
        v-model="mobile"
        type="tel"
        placeholder="请输入手机号"
        :rules="mobileRules"
      ></van-field>
      <van-field
        v-model="password"
        :type="`${show === false ? 'password' : 'text'}`"
        placeholder="请输入密码"
        :rules="passwordRules"
        v-if="isPass"
      >
        <template #button>
          <cp-icon
            :name="`login-eye-${show === false ? 'off' : 'on'}`"
            @click="show = !show"
          />
        </template>
      </van-field>
      <van-field
        v-else
        v-model="code"
        placeholder="短信验证码"
        :rules="codeRules"
      >
        <template #button>
          <span class="btn-send" :class="{ active: time > 0 }" @click="send">{{
            time > 0 ? `${time}s后再次发送` : '发送验证码'
          }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 尾部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <a
        class="icon"
        id="qq"
        @click="store.setReturnUrl(route.query.returnUrl as string)"
        :href="qqUrl"
      >
        <img src="@/assets/qq.svg" alt="" />
      </a>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
</style>
